package com.moli.iov.open.web.controller;

import com.moli.iov.base.model.po.VehicleBase;
import com.moli.iov.base.remote.IVehicleBaseRemoteService;
import com.moli.iov.base.service.IVehicleBaseService;
import com.moli.iov.his.dto.VehicleGpsVo;
import com.moli.iov.his.dto.VehicleTrajectoryReqDTO;
import com.moli.iov.his.dto.VehicleTrajectoryResDTO;
import com.moli.iov.his.enums.VehicleQueryType;
import com.moli.iov.his.remote.HistoryRemoteService;
import com.moli.iov.his.remote.IVehicleTrainTrajectoryRemoteService;
import com.moli.iov.his.service.IVehicleTrainTrajectoryService;
import com.moli.iov.open.model.vo.req.VehicleGpsParamQueryVo;
import com.moli.iov.open.model.vo.req.VehicleGpsQueryVo;
import com.moli.iov.rest.RestResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/his")
public class HisController {

    @Reference(group = "${service.group}", check = false)
    private HistoryRemoteService historyRemoteService;

    @Reference(group = "${service.group}", check = false)
    private IVehicleBaseRemoteService baseRemoteService;

    @Reference(group = "${service.group}", check = false)
    private IVehicleTrainTrajectoryRemoteService vehicleTrainTrajectoryRemoteService;

    @PostMapping("/track")
    public RestResponse<List<VehicleTrajectoryResDTO>> queryVehicleGpsByVinAndTime(@RequestBody @Validated VehicleGpsParamQueryVo vehicleGpsParamQueryVo) {
        List<VehicleGpsVo> vehicleGpsVos = null;
        String vin = null;
        try {
            log.info("----------queryVehicleGpsByVinAndTime请求入参：" + vehicleGpsParamQueryVo.toString());
            if ("1".equals(vehicleGpsParamQueryVo.getNumType())) {
                //1:代表车牌号
                //根据车牌号查询vin
                try {
                    RestResponse<String> vehicleBase = baseRemoteService.queryVinByLicense(vehicleGpsParamQueryVo.getNum());
                    if(RestResponse.isSuccess(vehicleBase)){
                        vin = vehicleBase.getData();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("根据车牌查询车辆vin信息接口异常....");
                    return RestResponse.failure("车辆查询失败");
                }
            } else {
                //2:代表车辆vin号码
                vin = vehicleGpsParamQueryVo.getNum();
            }
            if (null == vin) {
                return RestResponse.failure("查无车辆");
            } else {
                VehicleTrajectoryReqDTO dto = new VehicleTrajectoryReqDTO();
                dto.setEndTime(vehicleGpsParamQueryVo.getEndTime());
                dto.setNum(vehicleGpsParamQueryVo.getNum());
                dto.setNumType(vehicleGpsParamQueryVo.getNumType());
                dto.setStartTime(vehicleGpsParamQueryVo.getStartTime());
                long start = System.currentTimeMillis();
                RestResponse<List<VehicleTrajectoryResDTO>> res = historyRemoteService.listVehicleTrajectory(dto);
                log.info("****查询轨迹时长：{}",(System.currentTimeMillis()-start));
                List<VehicleTrajectoryResDTO> list = null;
                if(res!=null && res.getData()!=null ){
                    //车牌号
                    String license = null;
                    try {
                        VehicleBase vehicleBase = baseRemoteService.getVehicleBaseByVin(vin);
                        license = vehicleBase.getLicense();
                    } catch (Exception e) {
                        log.error("调用vehicleBaseService服务根据vin查询车辆信息异常....");
                        e.printStackTrace();
                        //return RestResponse.failure("查询失败,调用车辆基本信息查询服务异常....");
                    }

                    list = res.getData();
                    for (VehicleTrajectoryResDTO resDTO : list) {
                        resDTO.setVin(vin);
                        resDTO.setLicense(license);
                    }
                    return RestResponse.success("查询成功", list);
                }else {
                    return RestResponse.success("查询成功", list);
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failure("查询失败");
        }

    }

    /**
     * According vin to query vehicle trajectory.
     * @param vehicleGpsQueryVo Vehicle trajectory model.
     * @return Whether success.
     */
    @PostMapping("/trajectory")
    public RestResponse<List<String>> queryVehicleTrajectoryByVin(@RequestBody @Validated VehicleGpsQueryVo vehicleGpsQueryVo){
        String vin = null;
        if (VehicleQueryType.LICENSE.getCode().equals(vehicleGpsQueryVo.getNumType())) {
            try {
                RestResponse<String> vehicleBase = baseRemoteService.queryVinByLicense(vehicleGpsQueryVo.getNum());
                if(RestResponse.isSuccess(vehicleBase)){
                    vin = vehicleBase.getData();
                }
            } catch (Exception e) {
                log.error("根据车牌号查询车架号信息接口异常.... {}", e);
                return RestResponse.failure("车辆轨迹根据车牌号获取车架号失败");
            }
        } else {
            vin = vehicleGpsQueryVo.getNum();
        }
        if (null == vin) {
            return RestResponse.failure("查无车辆");
        } else {
            return vehicleTrainTrajectoryRemoteService.queryVehicleTrajectoriesByVin(vin);
        }
    }
}
